//
//  CanTingDetailViewController.m
//  MyProject
//
//  Created by apple on 15/11/9.
//  Copyright (c) 2015年 apple. All rights reserved.
// 美食 餐厅

#import "CanTingDetailViewController.h"
#import "AFNetworking.h"
#import "MeiShiCanTingModel.h"
#import "FirstTableViewCell.h"
#import "LZXHelper.h"
#define URL  @"http://www.youyuwei.com/api/dish?ywsdk_ver=20131018&app_code=com.youyuwei.yuwei.ios&oauth_timestamp=1447160055&oauth_version=1.0&oauth_nonce=822f7c9b3806a1c240846f833cb26574&restid=%@&oauth_signature=0U2MvjrQIHhlhNEaVbwiX5Cj9NY=&sys_ver=8.1.2&hardware_model=iPhone6,2&device_token=&oauth_consumer_key=4&ver=2&list=listofRest&oauth_signature_method=HMAC-SHA1&debug=0&app_ver=2.1&x_auth_mode=client_auth&oauth_token=0_9837387abc30183c39"
@interface CanTingDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    MeiShiCanTingModel *model;
}
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation CanTingDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self getData];
    
    [self creaetView];
}



-(void)getData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:[NSString stringWithFormat:URL,self.ID] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil][@"data"];

        
        NSDictionary *dicc = dic[@"restinfo"];
        model  = [[MeiShiCanTingModel alloc]initWithDictionary:dicc error:nil];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)creaetView
{
    self.tableView  =[[UITableView alloc]initWithFrame:self.view.bounds style:0];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //注册
    [self.tableView registerNib:[UINib nibWithNibName:@"FirstTableViewCell" bundle:nil] forCellReuseIdentifier:@"456"];
    //
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SecondTableViewCell" bundle:nil] forCellReuseIdentifier:@"678"];
    //
    [self.tableView registerNib:[UINib nibWithNibName:@"ThirdTableViewCell" bundle:nil] forCellReuseIdentifier:@"234"];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (!model) {
        return 0;
    }else{
        return 3;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if(indexPath.row == 0){
   FirstTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"456"];
        [cell1 createscrollViewWithimages:model.imgs];
        cell1.nameLable.text = model.name;
        cell1.titleLbale.text = model.name_origin;
        cell1.selectionStyle = 0;
        return cell1;
    }else if (indexPath.row == 1){
        cell = [tableView dequeueReusableCellWithIdentifier:@"678"];
        
        UILabel *lable1 = (UILabel *)[cell.contentView viewWithTag:1000];
        lable1.text = [NSString stringWithFormat:@"人均消费:%@",model.cost];
        
        UILabel *lable2 = (UILabel *)[cell.contentView viewWithTag:1001];
        lable2.text = [NSString stringWithFormat:@"开放时间:%@",model.open_time];
        
        UILabel *lable3 = (UILabel *)[cell.contentView viewWithTag:1002];
        lable3.text = [NSString stringWithFormat:@"电话:%@",model.phone];
        cell.selectionStyle = 0;
        return cell;
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"234"];
        
        UILabel *lable2 = (UILabel *)[cell.contentView viewWithTag:1000];
        lable2.text = model.desc;
        cell.selectionStyle = 0;
        return cell;
    }
    
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0){
    return 230;
    }else if (indexPath.row == 1){
        return 150;
    }else{
        float height = [LZXHelper textHeightFromTextString:model.desc width:[UIScreen mainScreen].bounds.size.width - 20  fontSize:15];
        return height + 50;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
